package com.bluetooth.demo.ui;

import android.app.Dialog;

import java.math.BigDecimal;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bluetooth.demo.R;
import com.bluetooth.demo.database.AsyncTaskRunner;
import com.bluetooth.demo.device.DeviceSettiingProfiles;
import com.bluetooth.demo.event.ClearLogEvent;
import com.bluetooth.demo.event.DeviceDataEvent;
import com.bluetooth.demo.event.DeviceInfoEvent;
import com.bluetooth.demo.event.DeviceLogEvent;
import com.bluetooth.demo.event.HeartEvent;
import com.bluetooth.demo.event.StepEvent;
import com.bluetooth.demo.model.UploadData;
import com.bluetooth.demo.ui.dialog.DialogUtils;
import com.bluetooth.demo.ui.dialog.IDialogActionListener;
import com.bluetooth.demo.ui.dialog.SelectFileDialog;
import com.bluetooth.demo.utils.DeviceDataUtils;
import com.bluetooth.demo.utils.FileUtils;
import com.bluetooth.demo.utils.HttpUtils;
import com.lifesense.ble.LsBleManager;
import com.lifesense.ble.OnDeviceReadListener;
import com.lifesense.ble.OnDeviceUpgradeListener;
import com.lifesense.ble.OnSettingListener;
import com.lifesense.ble.ReceiveDataCallback;
import com.lifesense.ble.bean.BloodGlucoseData;
import com.lifesense.ble.bean.BloodPressureData;
import com.lifesense.ble.bean.KitchenScaleData;
import com.lifesense.ble.bean.LsDeviceInfo;
import com.lifesense.ble.bean.PedometerData;
import com.lifesense.ble.bean.PedometerHeartRateData;
import com.lifesense.ble.bean.SportNotify;
import com.lifesense.ble.bean.WeightData_A2;
import com.lifesense.ble.bean.WeightData_A3;
import com.lifesense.ble.bean.WeightUserInfo;
import com.lifesense.ble.bean.constant.DeviceConnectState;
import com.lifesense.ble.bean.constant.DeviceUpgradeStatus;
import com.lifesense.ble.bean.constant.ManagerStatus;
import com.lifesense.ble.bean.constant.PacketProfile;
import com.lifesense.ble.bean.constant.ProtocolType;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import okhttp3.Call;

public class DeviceFragment extends Fragment {

    private static final String TAG = "LS-BLE";

    private View rootView;
    private TextView deviceNameView, stateTextView, batteryTextView, newDataTextView;
    private LsDeviceInfo currentDevice;
    private Dialog mDialog;
    private File mImageFile;
    private long startTime;
    private ProgressBar connectingProgressBar;
    private ProgressDialog upgradingDialog;
    private Handler mainHandler;
    private int mDataIndex;
    private DeviceSettingReceiver mSettingReceiver;
    private boolean hasSportNotify;
    private SportNotify mSportsNotify;
    private boolean isRealtimeDataShowing;
    private StepFragment mStepFragment;
    private HeartFragment mHeartFragment;
    private GradeFragment mGradeFragment;
    private MineFragment mMineFragment;
    private DebugFragment mDebugFragment;
    private OnSettingListener mSettingListener = new OnSettingListener() {
        @Override
        public void onFailure(int errorCode) {
            String msg = getResources().getString(R.string.setting_fail);
            DialogUtils.showToastMessage(getActivity(), msg + ",errorCode=" + errorCode);
        }

        @Override
        public void onSuccess(String mac) {
            String msg = getResources().getString(R.string.setting_ok);
            DialogUtils.showToastMessage(getActivity(), msg);
        }
    };
    private OnSettingListener mSportNotifyConfirmListener = new OnSettingListener() {
        @Override
        public void onSuccess(String macAddress) {
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    mSportsNotify = null;
                    String msg = getResources().getString(R.string.setting_ok);
                    DialogUtils.showToastMessage(getActivity(), msg);
                }
            });

        }

        @Override
        public void onFailure(final int errorCode) {
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    mSportsNotify = null;
                    String msg = getResources().getString(R.string.setting_fail);
                    DialogUtils.showToastMessage(getActivity(), msg + ",errorCode=" + errorCode);
                }
            });

        }
    };
    /**
     * 设备固件升级进度的回调对象
     */
    private OnDeviceUpgradeListener deviceUpgradeListener = new OnDeviceUpgradeListener() {

        @Override
        public void onDeviceUpdradeStateChange(String address, final DeviceUpgradeStatus upgradeStatus, final int errorCode) {
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (upgradeStatus == DeviceUpgradeStatus.UPGRADE_SUCCESS) {
                        long time = (System.currentTimeMillis() - startTime) / 1000;
                        int minute = (int) (time / 60);
                        int second = (int) (time % 60);
                        String content = getResources().getString(R.string.update_success);
                        content = content.replace("%m", minute + "");
                        content = content.replace("%s", second + "");
                        //cancel upgrading dialog
                        cancelUpgradingDialog();
                        DialogUtils.showPromptDialog(getActivity(), null, content);
                    } else if (upgradeStatus == DeviceUpgradeStatus.UPGRADE_FAILURE) {
                        String content = getResources().getString(R.string.update_fail);
                        content = content.replace("%@", "Code:" + errorCode);
                        //cancel upgrading dialog
                        cancelUpgradingDialog();
                        DialogUtils.showPromptDialog(getActivity(), null, content);
                    } else if (upgradeStatus == DeviceUpgradeStatus.UPGRADING
                            || upgradeStatus == DeviceUpgradeStatus.ENTER_UPGRADE_MODE) {
                        String msg = "upgrading.......";
                        if (DeviceUpgradeStatus.ENTER_UPGRADE_MODE == upgradeStatus) {
                            msg = "enter upgrade mode...";
                        }
                        showUpgradeMessage(msg);
                    }
                }
            });
        }

        @Override
        public void onDeviceUpgradeProcess(final int value) {
            String msg = "upgrading...." + value + "%";
            showUpgradeMessage(msg);

        }
    };
    /**
     * Device measurement data synchronization callback object
     */
    private ReceiveDataCallback mDataCallback = new ReceiveDataCallback() {
        @Override
        public void onDeviceConnectStateChange(DeviceConnectState connectState,
                                               String broadcastId) {
            //Device Connection Status
            updateDeviceConnectState(connectState);
        }

        @Override
        public void onReceiveWeightData_A3(WeightData_A3 wData) {
            LsBleManager.getInstance().setLogMessage("object data >> " + wData.toString());
            updateNewDatMessage();
            //for test
            if (wData.getImpedance() > 0) {
                DeviceSettiingProfiles.calculateBodyCompositionData(wData.getImpedance());
            }
            if (currentDevice.getProtocolType().equalsIgnoreCase(ProtocolType.A6.toString())) {
                //show realtime data if support
                shwoRealtimeData(wData);
            } else {
                /**
                 * Weight Scale Measurement Data
                 * A3 product
                 */
                EventBus.getDefault().post(new DeviceDataEvent(wData));
            }
        }

        @Override
        public void onReceiveUserInfo(WeightUserInfo proUserInfo) {
            /**
             * Weight Scale Product User Info
             * A3 product
             */
            EventBus.getDefault().post(new DeviceDataEvent(proUserInfo));
        }


        @Override
        public void onReceivePedometerMeasureData(final Object dataObject,
                                                  PacketProfile packetType, String sourceData) {
            int devicePower = DeviceDataUtils.getDevicePowerPercent(dataObject, packetType);
            updateDevicePower(devicePower);
            //update new data message
            updateNewDatMessage();
            if (dataObject instanceof PedometerHeartRateData) {
                final PedometerHeartRateData data = (PedometerHeartRateData) dataObject;
                final List heartRates = data.getHeartRates();
                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        EventBus.getDefault().post(new DeviceLogEvent(data.toString() + "\r\n"));
                        if (heartRates != null && !heartRates.isEmpty()) {
                            int heartrate = (Integer) heartRates.get(heartRates.size() - 1);
                            String na = data.getDeviceId();
                            Date medate = data.getMeasureTime();
                            SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            String medate1 = sformat.format(medate);

                            EventBus.getDefault().post(new HeartEvent(heartrate));

                            System.out.println("****************" + na + "***********" + heartrate + "***********" + medate1);
                            /**
                             * 上传心率
                             */
                            if (heartrate > 0) {
                                HttpUtils.upload(new UploadData(heartrate, 0, 0, na, medate1), new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {
                                        System.out.println("upload failed:" + e.getMessage());
                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
                                        System.out.println("upload succeed:" + response);
                                    }
                                });
                            }
                        } else {
                            EventBus.getDefault().post(new HeartEvent(0));
                        }
                    }
                });
            } else if (dataObject instanceof List) {
                List list = (List) dataObject;
                if (list != null && !list.isEmpty() && (list.get(0) instanceof PedometerData)) {
                    PedometerData pedometerData = (PedometerData) list.get(0);
                    Date medate2 = pedometerData.getMeasureTime();
                    SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String medate3 = sformat.format(medate2);
                    Double xxx = pedometerData.getCalories();
                    BigDecimal bg = new BigDecimal(xxx);
                    double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    System.out.println("**********************" + pedometerData.getWalkSteps() + "**********************" + medate3 + "**********************" + f1);
                    if (pedometerData.getWalkSteps() > 0) {
                        HttpUtils.upload(new UploadData(0, pedometerData.getWalkSteps(), f1, pedometerData.getDeviceId(), medate3), new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                System.out.println("upload failed:" + e.getMessage());
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                System.out.println("upload succeed:" + response);
                            }
                        });
                    }

                    EventBus.getDefault().post(new StepEvent(pedometerData.getWalkSteps(), pedometerData.getCalories()));

                }
            } else {
                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {

                        String text = dataObject.getClass().getSimpleName() + "\r\n";
                        text += "dataObject not instanceof PedometerHeartRateData\r\n";
                        EventBus.getDefault().post(new DeviceLogEvent(text));

                    }
                });
            }

            /**
             * Pedoemter Measurement Data
             * Product：BonbonC、Mambo、MamboCall、MamboHR、Mambo Watch、MT/Gold、ZIVA
             */
            EventBus.getDefault().post(new DeviceDataEvent(dataObject));

            LsBleManager.getInstance().setLogMessage("object data >> " + dataObject.toString());
        }


        @Override
        public void onReceiveWeightDta_A2(WeightData_A2 wData) {
            updateNewDatMessage();
            /**
             * Weight Scale Measurement Data
             * A2 product
             */
            EventBus.getDefault().post(new DeviceDataEvent(wData));
        }

        @Override
        public void onReceiveBloodPressureData(BloodPressureData bpData) {
            updateNewDatMessage();
            /**
             * Blood Pressure Measurement Data
             * A2/A3 product
             */
            EventBus.getDefault().post(new DeviceDataEvent(bpData));
        }

        @Override
        public void onReceivePedometerData(PedometerData pData) {
            if (pData.getBatteryPercent() > 0) {
                updateDevicePower(pData.getBatteryPercent());
            }
            updateNewDatMessage();
            /**
             * Pedometer Measurement Data
             * A2 product
             */
            EventBus.getDefault().post(new DeviceDataEvent(pData));
        }

        @Override
        public void onReceiveKitchenScaleData(KitchenScaleData kiScaleData) {
            updateNewDatMessage();
            /**
             * Kitchen Scale Measurement Data
             */
            EventBus.getDefault().post(new DeviceDataEvent(kiScaleData));
        }

        @Override
        public void onReceiveDeviceInfo(LsDeviceInfo lsDevice) {
            if (lsDevice == null || currentDevice == null) {
                return;
            }
            Log.e("LS-BLE", "Demo-Update Device Info:" + lsDevice.toString());
            //update and reset device's firmware version
            currentDevice.setFirmwareVersion(lsDevice.getFirmwareVersion());
            currentDevice.setHardwareVersion(lsDevice.getHardwareVersion());
            currentDevice.setModelNumber(lsDevice.getModelNumber());
            if (getActivity() != null) {
                //update and save device information
                AsyncTaskRunner runner = new AsyncTaskRunner(getActivity(), currentDevice);
                runner.execute();
                //show device information
                StringBuffer strBuffer = new StringBuffer();
                strBuffer.append("Device Version Information....,");
                strBuffer.append("ModelNumber:" + currentDevice.getModelNumber() + ",");
                strBuffer.append("firmwareVersion:" + currentDevice.getFirmwareVersion() + ",");
                strBuffer.append("hardwareVersion:" + currentDevice.getHardwareVersion() + ",");

                EventBus.getDefault().post(new DeviceDataEvent(strBuffer));
            }
        }

        @Override
        public void onPedometerSportsModeNotify(final String macAddress, final SportNotify sportNotify) {
            EventBus.getDefault().post(new DeviceDataEvent(sportNotify));
            if (mSportsNotify != null &&
                    mSportsNotify.getRequestType() == sportNotify.getRequestType()
                    && mSportsNotify.getSportsType() == sportNotify.getSportsType()) {
                Log.e("LS-BLE", "the same sport notify:" + sportNotify.toString() + "; local >> " + mSportsNotify.toString());
                return;
            }
            mSportsNotify = sportNotify;
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    //回复GPS的定位状态
                    DeviceSettiingProfiles.updatePhoneGpsStatus(macAddress, sportNotify, mSportNotifyConfirmListener);
                }
            });
        }

        @Override
        public void onReceiveBloodGlucoseData(BloodGlucoseData bgData) {
            updateNewDatMessage();
            EventBus.getDefault().post(new DeviceDataEvent(bgData));
        }


    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_device, container, false);
        deviceNameView = (TextView) rootView.findViewById(R.id.device_name_tv);
        stateTextView = (TextView) rootView.findViewById(R.id.device_connect_state_tv);
        newDataTextView = (TextView) rootView.findViewById(R.id.new_data_text_view);
        connectingProgressBar = (ProgressBar) rootView.findViewById(R.id.syncing_progress_bar);

        batteryTextView = (TextView) rootView.findViewById(R.id.device_battery_tv);

        batteryTextView.setText("");
        connectingProgressBar.setVisibility(View.GONE);
        setHasOptionsMenu(true);

//        showWhichFragment(R.id.rbtn_step);
        showWhichFragment(R.id.rbtn_heart);
        showWhichFragment(R.id.rbtn_grade);
        showWhichFragment(R.id.rbtn_mine);
        showWhichFragment(R.id.rbtn_debug);
        showWhichFragment(R.id.rbtn_step);

        RadioGroup rgroup = rootView.findViewById(R.id.rgroup);
        rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                showWhichFragment(checkedId);
            }
        });

        //注册设置回调广播
        mSettingReceiver = new DeviceSettingReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(DeviceSettiingProfiles.SETTING_PROFIES_ACTION);
        //注册广播
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mSettingReceiver, filter);
        return rootView;
    }

    protected void shwoRealtimeData(final WeightData_A3 wData) {
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                if (wData.isRealtimeData()) {
                    if (isRealtimeDataShowing) {
                        //update real time data
                        String msg = "Scale Realtime Data:" + wData.getWeight();
                        showUpgradeMessage(msg);
                        return;
                    } else {
                        isRealtimeDataShowing = true;
                        showUpgradingDialog(null, "Scale Realtime Data:" + wData.getWeight());
                    }
                } else {
                    cancelUpgradingDialog();
                    isRealtimeDataShowing = false;
                    //print data
                    EventBus.getDefault().post(new DeviceDataEvent(wData));
                }
            }
        });
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        logMessage("onCreate");
        mDataIndex = 0;
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        logMessage("onStart");
        super.onStart();
        Bundle args = getArguments();
        if (args != null && args.containsKey("DeviceInfo")) {
            currentDevice = args.getParcelable("DeviceInfo");
            //update test device info
            deviceNameView.setText(currentDevice.getDeviceName() + " [" + currentDevice.getMacAddress() + "]");
        }
        if (currentDevice != null && !TextUtils.isEmpty(currentDevice.getBroadcastID())) {
            //判断设备是否有缓存的测量数据
            List<Object> cacheDatas = DeviceDataUtils.getDeviceMeasurementData(currentDevice.getBroadcastID());
            if (cacheDatas == null || cacheDatas.size() == 0) {
                return;
            }
            for (Object obj : cacheDatas) {
                EventBus.getDefault().post(new DeviceDataEvent(obj));
            }
        }
        //清空缓存数据
        DeviceDataUtils.clearCacheData();

    }

    @Override
    public void onStop() {
        logMessage("onStop");
        super.onStop();
    }

    @Override
    public void onResume() {
        logMessage("onResume....");
        super.onResume();
        DeviceSettiingProfiles.initActivityContext(getActivity());
        if (mainHandler == null) {
            mainHandler = new Handler(getActivity().getMainLooper());
            DialogUtils.initDialogHandler(mainHandler);
        }
        //try to connected device
        connectDevice();
    }

    @Override
    public void onDestroy() {
        logMessage("onDestroy");
        super.onDestroy();
        LsBleManager.getInstance().stopDataReceiveService();
        if (getActivity() != null) {
            LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mSettingReceiver);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        if (DeviceSettiingProfiles.isNewProtocolDevice(currentDevice)) {
            inflater.inflate(R.menu.pedometer_functions, menu);
        } else if (currentDevice != null &&
                ProtocolType.A6.toString().equalsIgnoreCase(currentDevice.getProtocolType())) {
            inflater.inflate(R.menu.scale_functions, menu);
        } else {
            inflater.inflate(R.menu.common_functions, menu);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        handleDeviceFunction(item.getItemId());
        return super.onOptionsItemSelected(item);
    }

    private void connectDevice() {
        if (!LsBleManager.getInstance().isSupportLowEnergy()) {
            DialogUtils.showPromptDialog(getActivity(), "Prompt", "Unsupport Bluetooth Low Energy");
            return;
        }
        if (!LsBleManager.getInstance().isOpenBluetooth()) {
            DialogUtils.showPromptDialog(getActivity(), "Prompt", "Please turn on Bluetooth");
            return;
        }
        if (currentDevice == null) {
            DialogUtils.showPromptDialog(getActivity(), "Prompt", "No Devices!");
            return;
        }
        if (LsBleManager.getInstance().checkDeviceConnectState(currentDevice.getMacAddress()) == DeviceConnectState.CONNECTED_SUCCESS) {
            LsBleManager.getInstance().registerDataSyncCallback(mDataCallback);
            connectingProgressBar.setVisibility(View.GONE);
            stateTextView.setTextColor(Color.BLUE);
            stateTextView.setText(getResources().getString(R.string.state_connected));
            return;
        }
        if (LsBleManager.getInstance().getLsBleManagerStatus() == ManagerStatus.DATA_RECEIVE) {
            return;
        }
        LsBleManager.getInstance().stopDataReceiveService();
        //clear measure device list
        LsBleManager.getInstance().setMeasureDevice(null);
        //add target measurement device
        LsBleManager.getInstance().addMeasureDevice(currentDevice);
        //set product user info on data syncing mode
        DeviceSettiingProfiles.setProductUserInfoOnSyncingMode(currentDevice);
        //start data syncing service
        LsBleManager.getInstance().startDataReceiveService(mDataCallback);

        //update connect state
        updateDeviceConnectState(DeviceConnectState.CONNECTING);
    }

    /**
     * 更新数据提示信息
     */
    private void updateNewDatMessage() {
        if (getActivity() == null) {
            return;
        }
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                newDataTextView.clearAnimation();
                mDataIndex++;
                String newsStr = getResources().getString(R.string.str_new_data);
                newsStr = newsStr.replace("%@", mDataIndex + "");
                newDataTextView.setText(newsStr);
                newDataTextView.setTextColor(Color.RED);
                newDataTextView.setAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
            }
        });
    }

    /**
     * 更新设备电量显示
     *
     * @param percentage
     */
    private void updateDevicePower(final int percentage) {
        if (getActivity() == null) {
            return;
        }
        if (percentage <= 0) {
            return;
        }
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                batteryTextView.clearAnimation();
                String power = getResources().getString(R.string.str_battery_percentage);
                power = power.replace("%@", percentage + "%");
                batteryTextView.setText(power);
                batteryTextView.setVisibility(View.VISIBLE);
                batteryTextView.setAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
            }
        });
    }

    /**
     * 更新设备的连接状态信息
     *
     * @param connectState
     */
    private void updateDeviceConnectState(final DeviceConnectState connectState) {
        if (getActivity() == null) {
            return;
        }
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                if (DeviceConnectState.CONNECTED_SUCCESS == connectState) {
                    connectingProgressBar.setVisibility(View.GONE);
                    stateTextView.setTextColor(Color.BLUE);
                    newDataTextView.setVisibility(View.VISIBLE);
                    newDataTextView.setText(getResources().getString(R.string.str_no_data));
                    newDataTextView.setTextColor(Color.GRAY);
                    //清空文本信息
                    EventBus.getDefault().post(new ClearLogEvent());
                    stateTextView.setText(getResources().getString(R.string.state_connected));
                    DialogUtils.showToastMessage(getActivity(), "Successful connection.");
                } else {
                    mDataIndex = 0;
                    if (DeviceConnectState.DISCONNECTED == connectState) {
                        stateTextView.setTextColor(Color.RED);
                        stateTextView.setText(getResources().getString(R.string.state_disconnect));
                        DialogUtils.showToastMessage(getActivity(), stateTextView.getText().toString());
                    } else {
                        stateTextView.setTextColor(Color.GRAY);
                        stateTextView.setText(getResources().getString(R.string.state_connecting));
                    }
                    //
                    if (LsBleManager.getInstance().getLsBleManagerStatus() == ManagerStatus.DATA_RECEIVE) {
                        connectingProgressBar.setVisibility(View.VISIBLE);
                        newDataTextView.setVisibility(View.GONE);
                    } else {
                        newDataTextView.setVisibility(View.GONE);
                        connectingProgressBar.setVisibility(View.GONE);
                        stateTextView.setTextColor(Color.BLACK);
                        stateTextView.setText(getResources().getString(R.string.state_unknown));
                    }
                }
            }
        });
    }

    /**
     * log debug message
     *
     * @param msg
     */
    private void logMessage(String msg) {
        msg = DeviceFragment.class.getSimpleName() + " >> " + msg;
        Log.e(TAG, msg);
    }

    /**
     * 显示升级提示对话框
     *
     * @param title
     * @param message
     */
    private void showUpgradingDialog(String title, final String message) {
        ContextThemeWrapper ctw = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light);
        upgradingDialog = ProgressDialog.show(ctw, null, message, false);
        upgradingDialog.setCancelable(false);
        upgradingDialog.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {

            }
        });
    }

    /**
     * 显示升级提示信息
     *
     * @param msg
     */
    private void showUpgradeMessage(final String msg) {
        mainHandler.post(new Runnable() {

            @Override
            public void run() {
                if (upgradingDialog != null && upgradingDialog.isShowing()) {
                    upgradingDialog.setMessage(msg);
                }
            }
        });
    }

    /**
     * 取消升级提示Dialog
     */
    private void cancelUpgradingDialog() {
        if (upgradingDialog != null && upgradingDialog.isShowing()) {
            upgradingDialog.dismiss();
        }
    }

    /**
     * upgrade device firmware
     */
    private void upgradeDevice() {
        /**
         * stop all data syncing or previous upgrading,make sure the status of sdk is idle
         */
        LsBleManager.getInstance().stopDataReceiveService();
        LsBleManager.getInstance().interruptUpgradeProcess(currentDevice.getMacAddress());
        //get upgrade file from assets folder
        final List<File> fileList = FileUtils.getUpgradeFileFromSDcard();//FileTools.getUpgradeFileFromAssets(getActivity(),currentDevice);
        if (fileList == null || fileList.size() == 0) {
            DialogUtils.showPromptDialog(getActivity(), "Upgrade Failure", "File Not Found!");
            return;
        }
        //show upgrading dialog
        mDialog = new SelectFileDialog.Builder(getActivity())
                .setFileList(fileList)
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //save upgrade file in SD card if need
                        mImageFile = fileList.get(position);//FileTools.saveUpgradeFile(getActivity(),fileList.get(position).getName());
                        stateTextView.setText(mImageFile.getName());
                        mDialog.dismiss();
                        //calling sdk interface
                        LsBleManager.getInstance().upgradeDeviceFirmware(currentDevice.getMacAddress(), mImageFile, deviceUpgradeListener);
                        startTime = System.currentTimeMillis();
                        cancelUpgradingDialog();
                        showUpgradingDialog(null, "upgrade firmware...");
                        connectingProgressBar.setVisibility(View.GONE);
                    }
                }).create();
        mDialog.show();
    }

    /**
     * 处理设备功能
     *
     * @param index
     */
    private void handleDeviceFunction(int index) {
        String deviceMac = currentDevice.getMacAddress();
        if (TextUtils.isEmpty(deviceMac)) {
            Toast.makeText(getActivity(), "undefined.....", Toast.LENGTH_LONG).show();
            return;
        }
        switch (index) {
            case R.id.fun_device_info: {
                //设备信息
                EventBus.getDefault().post(new DeviceInfoEvent(currentDevice));
            }
            break;
            case R.id.fun_clear: {
                //清空文本信息
                EventBus.getDefault().post(new ClearLogEvent());
            }
            break;
            case R.id.fun_upgrade: {
                //固件升级
                upgradeDevice();
            }
            break;
            case R.id.fun_alarm_clock: {
                //闹钟提醒
                DeviceSettiingProfiles.updateAlarmClockWithoutTitle(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_sedentry: {
                //久坐提醒
                DeviceSettiingProfiles.updateSedentaryInfo(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_event_reminder: {
                //事件提醒
                DeviceSettiingProfiles.updateEventRemind(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_night_mode: {
                //夜间模式
                DeviceSettiingProfiles.updateNightDisplayMode(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_screen_mode: {
                //屏幕显示
                DeviceSettiingProfiles.updateScreenDisplayMode(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_wear_mode: {
                //佩戴方式
                DeviceSettiingProfiles.updateWearingStyles(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_hr_detect_mode: {
                //心率检测
                DeviceSettiingProfiles.updateHeartDetectionMode(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_weather: {
                //天气提醒
                DeviceSettiingProfiles.updateWeater(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_dictance_unit: {
                //距离显示
                DeviceSettiingProfiles.updateDistanceUnit(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_time_unit: {
                //时间显示
                DeviceSettiingProfiles.updateTimeFormat(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_dialpace_mode: {
                //表盘样式
                DeviceSettiingProfiles.updateDialpace(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_auto_discern: {
                //自动识别
                DeviceSettiingProfiles.updateAutoRecognition(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_hr_warning: {
                //心率预警
                DeviceSettiingProfiles.updateHeartRateWarning(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_behavior_reminder: {
                //行为提醒
                DeviceSettiingProfiles.updateBehaviorRemind(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_target: {
                //目标设置
                DeviceSettiingProfiles.updateEncourage(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_read_battery: {
                //读取电量
                boolean isSuccess = DeviceSettiingProfiles.readDeviceBattery(deviceMac, new OnDeviceReadListener() {
                    @Override
                    public void onDeviceVoltageValue(byte[] srcData, int flag, float vlotage, int percentage) {
                        //update device voltage
                        updateDevicePower(percentage);
                        DialogUtils.showToastMessage(getActivity(), "vlotage=" + vlotage + "; percentage" + percentage);
                    }
                });
                if (!isSuccess) {
                    DialogUtils.showToastMessage(getActivity(), getResources().getString(R.string.str_device_not_connected));
                }
            }
            break;
            case R.id.fun_custom_pages: {
                //自定义页面
                DeviceSettiingProfiles.updateCustomPage(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_app_permission: {
                //应用权限设置
                DeviceSettiingProfiles.showAppPermissionSetting(new IDialogActionListener() {
                    @Override
                    public void onIntentResults(Intent intent) {
                        if (intent != null) {
                            startActivity(intent);
                        }
                    }
                });
            }
            break;
            case R.id.fun_connect: {
                //连接，同步数据
                connectDevice();
            }
            break;
            case R.id.fun_cancel_connect: {
                //断开连接
                LsBleManager.getInstance().stopDataReceiveService();
                updateDeviceConnectState(DeviceConnectState.DISCONNECTED);
            }
            break;
            case R.id.fun_message_remind: {
                //更新消息提醒设置
                DeviceSettiingProfiles.updateMessageRemind(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_test_message_remind: {
                //模拟应用消息通知，测试消息提醒
                DeviceSettiingProfiles.testMessageRemind();
            }
            break;
            case R.id.fun_test_incoming_call: {
                //模拟手机来电信息，测试来电功能
                DeviceSettiingProfiles.testIncomingCall(deviceMac);
            }
            break;
            case R.id.fun_user_info: {
                //更新秤的用户信息
                DeviceSettiingProfiles.updateScaleUserInfo(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_bind_device: {
                //测试绑定功能
                cancelUpgradingDialog();
                showUpgradingDialog(null, "binding device...");
                DeviceSettiingProfiles.bindingDevice(currentDevice, new IDialogActionListener() {
                    @Override
                    public void onBindingResults() {
                        cancelUpgradingDialog();
                    }

                });
            }
            break;
            case R.id.fun_health_score: {
                //更新健康信息
                DeviceSettiingProfiles.updateHealthScoreInfo(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_device_positioning: {
                //寻找手环功能，设备定位
                DeviceSettiingProfiles.testDevicePositioning(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_photographing: {
                //远程拍摄
                DeviceSettiingProfiles.testRemotePhotographing(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_device_restart: {
                //重启设备
                DeviceSettiingProfiles.restartDevice(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_device_switch: {
                //更新设备开关状态信息
                DeviceSettiingProfiles.updateDeviceFunctionStatus(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_sport_request: {
                //推送运动请求信息
                DeviceSettiingProfiles.sendSportRequest(deviceMac, mSettingListener);
            }
            break;
            case R.id.fun_mood_reminder: {
                //设置心情记录提示
                DeviceSettiingProfiles.updateMoodbeamReminder(deviceMac, mSettingListener);
            }
            break;
        }
    }

    private void showWhichFragment(int id) {
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        hideAllFragment(fragmentTransaction);
        switch (id) {
            case R.id.rbtn_step:
                if (mStepFragment == null) {
                    mStepFragment = StepFragment.newInstance();
                    fragmentTransaction.add(R.id.layout_fragment, mStepFragment);
                } else {
                    fragmentTransaction.show(mStepFragment);
                }
                break;
            case R.id.rbtn_heart:
                if (mHeartFragment == null) {
                    mHeartFragment = HeartFragment.newInstance();
                    fragmentTransaction.add(R.id.layout_fragment, mHeartFragment);
                } else {
                    fragmentTransaction.show(mHeartFragment);
                }
                break;
            case R.id.rbtn_grade:
                if (mGradeFragment == null) {
                    mGradeFragment = GradeFragment.newInstance();
                    fragmentTransaction.add(R.id.layout_fragment, mGradeFragment);
                } else {
                    fragmentTransaction.show(mGradeFragment);
                }
                break;
            case R.id.rbtn_mine:
                if (mMineFragment == null) {
                    mMineFragment = MineFragment.newInstance();
                    fragmentTransaction.add(R.id.layout_fragment, mMineFragment);
                } else {
                    fragmentTransaction.show(mMineFragment);
                }
                break;
            case R.id.rbtn_debug:
                if (mDebugFragment == null) {
                    mDebugFragment = DebugFragment.newInstance();
                    fragmentTransaction.add(R.id.layout_fragment, mDebugFragment);
                } else {
                    fragmentTransaction.show(mDebugFragment);
                }
                break;
            default:
                break;


        }
        fragmentTransaction.commit();
    }

    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (mStepFragment != null) {
            fragmentTransaction.hide(mStepFragment);
        }
        if (mHeartFragment != null) {
            fragmentTransaction.hide(mHeartFragment);
        }
        if (mGradeFragment != null) {
            fragmentTransaction.hide(mGradeFragment);
        }
        if (mMineFragment != null) {
            fragmentTransaction.hide(mMineFragment);
        }
        if (mDebugFragment != null) {
            fragmentTransaction.hide(mDebugFragment);
        }
    }

    private class DeviceSettingReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            logMessage("broadcast receiver >>" + intent);
            if (intent == null) {
                return;
            }
            if (!DeviceSettiingProfiles.SETTING_PROFIES_ACTION.equalsIgnoreCase(intent.getAction().toString())) {
                return;
            }
            String msg = intent.getStringExtra("errorMsg");
            logMessage("broadcast resutls >>" + msg);
            EventBus.getDefault().post(msg);
        }

    }
}
